﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;
using ECS.Framework;
using ECS.BLL.Component;
using ECS.BLL.Entity;
using ECS.DAL.Component;
using ECS.DAL.Entity;
using ECS.DAL;
using System.Data.Common;
namespace E_ICS.E_ICS
{
    public partial class frmAddMAWB : Form
    {
        #region private properties
        string stype = string.Empty;
        string reg = @"^[A-Za-z0-9\b\cC\cV\cX]*$";
        string appId = string.Empty;
        string device = string.Empty;
        //string stationID = "Station1";
        //SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["con2"].ConnectionString);
        Main.frmMain2 main;
        int seq = 1;
        string Carrier, fltNo, fltdate, origin, dest, naturegood, piecs, prefix, serial, fltno_display, mawb, hawb, fltdate_display, shipment_type;
        string ewsFlag = "NO";
        string slip_type = "LOCAL";
        DataSet dataset = null;
        //SqlConnection sqlCon = null;
        #endregion

        #region form events
        public frmAddMAWB(Main.frmMain2 _main)
        {
            InitializeComponent();
            main = _main;

        }
        private void txtHAWB_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!(Regex.IsMatch(e.KeyChar.ToString(), reg)))//|| (e.KeyChar == (char)Keys.Back)))
                {
                    e.Handled = true;
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        //private void transhipment_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (transhipment.Checked)
        //    {
        //       slip_type = "Transhipment";
        //       DateFormat();
        //       txtCarrier.Text = "BFS";
        //       txtFltNo.Text = "001";
        //       txtFltdate.Text = fltdate_display;
        //       txtNatureGood.Text = "TRANS";
        //       txtOrigin.Text = string.Empty;
        //    }
        //    else
        //    {
        //       slip_type = "Local";
        //       txtOrigin.Text = "BKK";
        //       txtCarrier.Text = String.Empty;
        //       txtFltNo.Text = String.Empty;
        //       txtFltdate.Text = String.Empty;
        //       txtNatureGood.Text = String.Empty;
        //    }
        //    //CommonMessageBox.ShowMessageInfo(slip_type);
        //}

        private void EWS_CheckedChanged(object sender, EventArgs e)
        {
            if (EWS.Checked)
            {
                ewsFlag = "YES";
            }
            else
            {
                ewsFlag = "NO";
            }
            //CommonMessageBox.ShowMessageInfo(ewsFlag);
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            frmFlightSearch fs = new frmFlightSearch();
            fs.Show();

        }

        private void frmAddMAWB_Load(object sender, EventArgs e)
        {
            SetScreen();
            try
            {
                appId = ConfigurationManager.AppSettings["appID"];
                device = ConfigurationManager.AppSettings["device"];
                //sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["con3"].ConnectionString);
                txtPrefix.Focus();

                SetScnFromfrmMAWBsearch();
                LoadDrobdrown();
                SetDefaultLoadDrobdrown();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }

        private void SetScreen()
        {
            this.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_frmAddMAWB");
            lblShipmentType.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_lblShipmentType");
            radioButton1.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_radioButton1");
            radioButton2.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_radioButton2");
            lblMAWB.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_lblMAWB");
            lblHAWB.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_lblHAWB");
            label1.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_label1");
            lblFlightNO.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_lblFlightNO");
            label3.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_label3");
            lblDest.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_lblOrigin");
            label6.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_lblDest");
            EWS.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_chk_EWS");
            label5.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_label5");
            label4.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_label4");
            btnAdd.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_btnAdd");
            btnCancel.Text = CommonResourceString.GetResourseValue("scn_AddMAWB_btnCancel");


        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ///this.DialogResult = DialogResult.OK;
                if (radioButton1.Checked)
                {
                    stype = "CONSOL";
                    if (Requirecon())
                    {
                        if (ValidateMawbAdd())
                        {
                            DateTime date;
                            if (!string.IsNullOrEmpty(txtFltdate.Text))
                            {
                                if (DateTime.TryParse(txtFltdate.Text, out date) == false)
                                {
                                    CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_AddMAWB_msgBox_Ck_Flght_date"));
                                    //CommonMessageBox.ShowMessageWarning("Flightdate format is not correct.");
                                    txtFltdate.Focus();
                                    return;
                                }
                            }
                            if (!ValidateShipmentType(stype))
                            {
                                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));
                            }
                            else
                            {
                                checkMAWB();
                                this.DialogResult = DialogResult.OK;
                                this.Close();
                            }
                            //if (slip_type == "LOCAL")
                            //{
                            //    if (!ValidateShipmentType(stype))
                            //    {
                            //        CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));
                            //    }
                            //    else
                            //    {
                            //        checkMAWB();
                            //        this.DialogResult = DialogResult.OK;
                            //        this.Close();
                            //    }
                            //}
                            //else
                            //{
                            //    checkMAWB();
                            //    this.DialogResult = DialogResult.OK;
                            //    this.Close();
                            //}
                        }
                        else
                        {
                            CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_AddMAWB_msgBox_Ck_MAWB"));
                            //CommonMessageBox.ShowMessageWarning("Invalid MAWB Check Digit");
                        }
                        ClearValueToAddMAWB();
                    }
                    else
                    {
                        CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_AddMAWB_msgBox_Ck_Required"));
                        //CommonMessageBox.ShowMessageWarning("Required fields are missing");
                    }
                }
                else
                {
                    stype = "NONCON";
                    if (Requirenoncon())
                    {
                        if (ValidateMawbAdd())
                        {
                            DateTime date;
                            if (!string.IsNullOrEmpty(txtFltdate.Text))
                            {
                                if (DateTime.TryParse(txtFltdate.Text, out date) == false)
                                {
                                    CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_AddMAWB_msgBox_Ck_Flght_date"));
                                    //CommonMessageBox.ShowMessageWarning("Flightdate format is not correct.");
                                    txtFltdate.Focus();
                                    return;
                                }
                            }
                            if (!ValidateShipmentType(stype))
                            {
                                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));
                            }
                            else
                            {
                                checkMAWB();
                                this.DialogResult = DialogResult.OK;
                                this.Close();
                            }
                            //if (slip_type == "LOCAL")
                            //{
                            //    if (!ValidateShipmentType(stype))
                            //    {
                            //        CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));
                            //    }
                            //    else
                            //    {
                            //        checkMAWB();
                            //        this.DialogResult = DialogResult.OK;
                            //        this.Close();
                            //    }
                            //}
                            //else
                            //{
                            //    checkMAWB();
                            //    this.DialogResult = DialogResult.OK;
                            //    this.Close();
                            //}
                        }
                        else
                        {
                            CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_AddMAWB_msgBox_Ck_MAWB"));
                            //CommonMessageBox.ShowMessageWarning("Invalid MAWB Check Digit");
                        }
                        ClearValueToAddMAWB();
                    }
                    else
                    {
                        CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_AddMAWB_msgBox_Ck_Required"));
                        //CommonMessageBox.ShowMessageWarning("Required fields are missing");
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                txtHAWB.Enabled = false;
                txtHAWB.Text = string.Empty;
                shipment_type = "NONCON";
            }
            else
            {
                txtHAWB.Enabled = true;
                shipment_type = "CONSOL";
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                txtHAWB.Enabled = false;
                txtHAWB.Text = string.Empty;
                shipment_type = "NONCON";
            }
            else
            {
                txtHAWB.Enabled = true;
                shipment_type = "CONSOL";
            }
        }
        private void txtPrefix_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
            {
                e.Handled = true;
            }
        }

        private void txtSerial_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
            {
                e.Handled = true;
            }
        }
        private void txtFltNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
            {
                e.Handled = true;
            }
        }


        private void txtPieces_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
            {
                e.Handled = true;
            }
        }
        //private void dpkFlightDate_ValueChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        DateFormat();
        //        txtFltdate.Text = fltdate_display;
        //    }
        //    catch (Exception ex)
        //    {
        //        CommonMessageBox.ShowMessageError(ex);
        //    }
        //}

        private void dpkFlightDate_CloseUp(object sender, EventArgs e)
        {
            try
            {
                DateFormat();
                txtFltdate.Text = fltdate_display;
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion
        #region private methos
        private bool ValidateShipmentType(string type)
        {
            bool result = true;
            if (type != GlobalVariable.chkShipmentType)
            {
                if (GlobalVariable.chkShipmentType == "")
                {
                    result = true;
                }
                else if (GlobalVariable.chkDatasource == "CARGOSPOT")
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            return result;
        }
        private void LoadDrobdrown()
        {
            try
            {
                List<ComboItem> items = new List<ComboItem>();
                DataSet ds = new DataSet();
                using (Entities en = new Entities())
                {
                    en.Open();
                    DbCommand cmd = en.CreateCommand("usp_GetSetupRecord");
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(en.CreateParameter("@in_vchApplicationID", GlobalVariable.user.ApplicationID));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchDevice", GlobalVariable.user.Device));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchUserID", GlobalVariable.user.UserID));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchStationID", "0"));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchSettingName", "SLIP_TYPE"));
                    DbDataAdapter adapter = en.CreateDataAdapter(cmd);
                    adapter.Fill(ds);
                    string status = ds.Tables[0].Rows[0]["Status"].ToString();
                    string message = ds.Tables[0].Rows[0]["Message"].ToString();
                    if (status == "PASS")
                    {
                        //for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
                        //{
                        //    ComboItem item = new ComboItem();
                        //    item.text = ds.Tables[0].Rows[i]["setting_value_string"].ToString();
                        //    item.value = ds.Tables[0].Rows[i]["setting_value_string"].ToString();
                        //    items.Add(item);
                        //}

                        cboSlipType.DisplayMember = "setting_value_string";
                        cboSlipType.ValueMember = "setting_value_string";
                        cboSlipType.DataSource = ds.Tables[0];
                        //if (items != null && items.Count > 0)
                        //{
                        //    cboSlipType.DataSource = items;
                        //    cboSlipType.ValueMember = "value";
                        //    cboSlipType.DisplayMember = "text";
                        //}
                        if (slip_type == "LOCAL")
                        {
                            txtOrigin.Text = "BKK";
                            txtCarrier.Text = String.Empty;
                            txtFltNo.Text = String.Empty;
                            txtFltdate.Text = String.Empty;
                            txtNatureGood.Text = String.Empty;
                        }
                        else if (slip_type == "TRANSHIPMENT")
                        {
                            DateFormat();
                            txtCarrier.Text = "BFS";
                            txtFltNo.Text = "001";
                            txtFltdate.Text = fltdate_display;
                            txtNatureGood.Text = "TRANS";
                            txtOrigin.Text = string.Empty;
                        }
                        else if (slip_type == "RE-STORAGE")
                        {
                            //
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }


        private void SetDefaultLoadDrobdrown()
        {
            try
            {
                List<ComboItem> items = new List<ComboItem>();
                DataSet ds = new DataSet();
                using (Entities en = new Entities())
                {
                    en.Open();
                    DbCommand cmd = en.CreateCommand("usp_GetSetupRecord");
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(en.CreateParameter("@in_vchApplicationID", GlobalVariable.user.ApplicationID));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchDevice", GlobalVariable.user.Device));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchUserID", GlobalVariable.user.UserID));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchStationID", GlobalVariable.user.Device));
                    cmd.Parameters.Add(en.CreateParameter("@in_vchSettingName", "SLIP_TYPE"));
                    DbDataAdapter adapter = en.CreateDataAdapter(cmd);
                    adapter.Fill(ds);
                    string status = ds.Tables[0].Rows[0]["Status"].ToString();
                    string message = ds.Tables[0].Rows[0]["Message"].ToString();
                    if (status == "PASS")
                    {

                        cboSlipType.SelectedValue = ds.Tables[0].Rows[0]["setting_value_string"].ToString();

                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void SetScnFromfrmMAWBsearch()
        {
            txtSerial.Text = ValueTofrmAddMAWB.SelSerial;
            txtPrefix.Text = ValueTofrmAddMAWB.SelPrefix;
            txtHAWB.Text = ValueTofrmAddMAWB.Selhawb;
            txtPieces.Text = ValueTofrmAddMAWB.Selpiece_count;
            txtCarrier.Text = ValueTofrmAddMAWB.Selcarrier;
            txtFltNo.Text = ValueTofrmAddMAWB.SelFlt_no;
            txtOrigin.Text = ValueTofrmAddMAWB.Seldest;
            txtNatureGood.Text = ValueTofrmAddMAWB.SelNatureOfGood;
            if (ValueTofrmAddMAWB.consol)
            {
                radioButton1.Checked = true;
                txtOrigin.Text = "BKK";
                //transhipment.Checked = false;
                txtCarrier.Text = String.Empty;
                txtFltNo.Text = String.Empty;
                txtFltdate.Text = String.Empty;
                txtNatureGood.Text = String.Empty;
            }
            else
            {
                radioButton2.Checked = true;
                txtOrigin.Text = "BKK";
                //transhipment.Checked = false;
                txtCarrier.Text = String.Empty;
                txtFltNo.Text = String.Empty;
                txtFltdate.Text = String.Empty;
                txtNatureGood.Text = String.Empty;
            }
            //if (ValueTofrmAddMAWB.SelslipType.Equals("Shipment-type"))
            //{
            //    DateFormat();
            //    //transhipment.Checked = true;
            //    txtCarrier.Text = "BFS";
            //    txtFltNo.Text = "001";
            //    txtFltdate.Text = fltdate_display;
            //    txtNatureGood.Text = "TRANS";
            //    txtOrigin.Text = string.Empty;
            //}
            //else if (ValueTofrmAddMAWB.SelslipType.Equals("Local"))
            //{
            //    txtOrigin.Text = "BKK";
            //    //transhipment.Checked = false;
            //    txtCarrier.Text = String.Empty;
            //    txtFltNo.Text = String.Empty;
            //    txtFltdate.Text = String.Empty;
            //    txtNatureGood.Text = String.Empty;
            //}        
            //if (txtHAWB.Text.Equals(""))
            //{
            //    radioButton1.Checked = false;
            //    radioButton2.Checked = true;
            //}
            //else
            //{
            //    radioButton1.Checked = true;
            //    radioButton2.Checked = false;
            //}
            //dpkFlightDate.Value = Convert.ToDateTime(ValueTofrmAddMAWB.Selflight_date);
        }

        private void ClearValueToAddMAWB()
        {
            ValueTofrmAddMAWB.SelPrefix = "";
            ValueTofrmAddMAWB.SelSerial = "";
            ValueTofrmAddMAWB.Selhawb = "";
            ValueTofrmAddMAWB.SelFlt_no = "";
            ValueTofrmAddMAWB.SelFlt_date = "";
            ValueTofrmAddMAWB.Seldest = "";
            ValueTofrmAddMAWB.SelSeq = "";
            ValueTofrmAddMAWB.SelAgentCode = "";
            ValueTofrmAddMAWB.SelAgentName = "";
            ValueTofrmAddMAWB.SelNatureOfGood = "";
            ValueTofrmAddMAWB.Selshipment_type = "";
            ValueTofrmAddMAWB.Selpiece_count = "";
            ValueTofrmAddMAWB.Selcarrier = "";
            ValueTofrmAddMAWB.Selflight_date = "";
            ValueTofrmAddMAWB.SelslipType = "";
            ValueTofrmAddMAWB.SelOrigin = string.Empty;
        }

        private bool ValidateMawbAdd()
        {
            try
            {
                string serial = txtSerial.Text;

                if ((serial.Length == 8) || (serial.Length == 13))
                {

                    string lastdigit = serial.Substring(7, 1);
                    int intlastdigit = Convert.ToInt32(lastdigit);
                    int intserial = Convert.ToInt32(serial.Substring(0, 7));
                    int cklastDig = intserial % 7;
                    if (intlastdigit == cklastDig)
                    {
                        return true;
                    }
                    else
                    {

                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {

                return false;

            }

        }

        private void CreateMAWBMessage()
        {
            string path = "";
            SearchInfo search = new SearchInfo();
            path = search.GetBFLPath();
            Export.WriteMawbFile(path, fltdate.Substring(0, 5), GlobalVariable.fbl_fltNO, fltno_display, origin, dest, prefix, serial, piecs, naturegood, slip_type);
            //Export.WriteMawbFile(@"C:\FBL", fltdate.Substring(0, 5),GlobalVariable.fbl_fltNO, fltno_display,origin,dest, prefix, serial, piecs, naturegood);
            //SqlCommand cmd = new SqlCommand("SELECT setting_value_string FROM t_eics_setup where setting_name = 'FBL_FOLDER'", sqlCon);
            //SqlDataAdapter DA = new SqlDataAdapter(cmd);
            //try
            //{
            //    sqlCon.Open();
            //    SqlDataReader reader3 = cmd.ExecuteReader();
            //    if (reader3.Read())
            //    {
            //        path = reader3["setting_value_string"].ToString();
            //        Export.WriteMawbFile(path, fltdate.Substring(0, 5), GlobalVariable.fbl_fltNO, fltno_display, dest, prefix, serial, piecs, naturegood);
            //    }
            //    else
            //    {
            //        sqlCon.Close();
            //    }
            //}
            //catch (Exception)
            //{
            //    //
            //}
            //finally
            //{
            //    if (sqlCon != null && sqlCon.State != ConnectionState.Closed)
            //    {
            //        sqlCon.Close();
            //    }
            //}


            //Export.WriteMawbFile(@"C:\FBL", fltdate.Substring(0, 5), fltno_display, dest, prefix, serial, piecs, naturegood);

            //string a = fltdate.Substring(0, 5);
            //string directory = "C:/FBL";
            //if (!Directory.Exists("C:/FBL"))
            //{
            //    Directory.CreateDirectory("C:/FBL");
            //}
            //String filename = "FBL_" + fltno_display + "_" + a + ".txt";
            //string path = Path.Combine(directory,filename);
            //StreamWriter File = new StreamWriter(path);
            //File.WriteLine("FBL/3");
            //File.Write("1/");
            //File.Write("CX1000/");
            //File.Write(a);
            //File.Write("/");
            //File.WriteLine("BKK");
            //File.WriteLine(dest);
            //File.Write(prefix);
            //File.Write("-");
            //File.Write(serial);
            //File.Write("BKK");
            //File.Write(dest);
            //File.Write("/");
            //File.Write("P");
            //File.Write(piecs);
            //File.Write("K");
            //File.Write(0);
            //File.Write("T");
            //File.Write(0);
            //File.Write("/");
            //File.WriteLine(naturegood);
            //File.Write("LAST");
            //File.Close();
        }
        private void DateFormat()
        {
            SearchInfo search = new SearchInfo();
            fltdate_display = search.GetDateFormat(dpkFlightDate.Value);
            //SqlCommand cmd = new SqlCommand("select dbo.usf_format_flight_date(@date) as [Flight Date Display]", sqlCon);
            //SqlDataAdapter DA = new SqlDataAdapter(cmd);
            //cmd.Parameters.AddWithValue("@date", dpkFlightDate.Value);
            //sqlCon.Open();
            //SqlDataReader reader2 = cmd.ExecuteReader();
            //if (reader2.Read())
            //{
            //    fltdate_display = reader2["Flight Date Display"].ToString();
            //}
            //else
            //{
            //    sqlCon.Close();
            //}
            //sqlCon.Close();
        }




        private void checkconsol()
        {
            if (String.IsNullOrEmpty(txtHAWB.Text))
            {
                shipment_type = "NONCON";

            }
            else
            {
                shipment_type = "CONSOL";

            }
        }

        private bool Requirecon()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtPrefix.Text) && !string.IsNullOrEmpty(txtSerial.Text)
               && !string.IsNullOrEmpty(txtHAWB.Text) && !string.IsNullOrEmpty(txtCarrier.Text)
                && !string.IsNullOrEmpty(txtFltNo.Text) && !string.IsNullOrEmpty(txtFltdate.Text)
                && !string.IsNullOrEmpty(txtOrigin.Text) && !string.IsNullOrEmpty(txtDest.Text)
                && !string.IsNullOrEmpty(txtNatureGood.Text) && !string.IsNullOrEmpty(txtPieces.Text))
                {
                    return true;
                }
                //if (txtPrefix.Text != string.Empty && txtSerial.Text != string.Empty
                //   && txtHAWB.Text != string.Empty && txtCarrier.Text != string.Empty
                //    && txtFltNo.Text != string.Empty && txtDest.Text != string.Empty
                //    && txtNatureGood.Text != string.Empty && txtPieces.Text != string.Empty)
                //{
                //    return true;
                //}
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {

                return false;
            }

        }
        private bool Requirenoncon()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtPrefix.Text) && !string.IsNullOrEmpty(txtSerial.Text)
                && !string.IsNullOrEmpty(txtCarrier.Text) && !string.IsNullOrEmpty(txtFltNo.Text)
                && !string.IsNullOrEmpty(txtFltdate.Text) && !string.IsNullOrEmpty(txtOrigin.Text)
                && !string.IsNullOrEmpty(txtDest.Text) && !string.IsNullOrEmpty(txtNatureGood.Text)
                && !string.IsNullOrEmpty(txtPieces.Text))
                {
                    return true;
                }
                //if (txtPrefix.Text != string.Empty && txtSerial.Text != string.Empty
                //   && txtHAWB.Text != string.Empty && txtCarrier.Text != string.Empty
                //    && txtFltNo.Text != string.Empty && txtDest.Text != string.Empty
                //    && txtNatureGood.Text != string.Empty && txtPieces.Text != string.Empty)
                //{
                //    return true;
                //}
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {

                return false;
            }
        }
        private void checkMAWB()
        {
            string errorMessage = string.Empty;
            string status = string.Empty;
            //SearchInfo search = new SearchInfo();
            //SearchCriteria criteria = new SearchCriteria();
            //criteria.AppId = GlobalVariable.user.ApplicationID;
            //criteria.Device = GlobalVariable.user.Device;
            //criteria.UserId = GlobalVariable.user.UserID;
            //criteria.MAWB = txtPrefix.Text + txtSerial.Text; ;
            //criteria.MAWBPrefix = txtPrefix.Text;
            //criteria.MAWBSerial = txtSerial.Text;
            //criteria.HAWB = txtHAWB.Text;
            //dataset = search.GetMAWB(criteria);
            SearchInfo search = new SearchInfo();
            SearchCriteria criteria = new SearchCriteria();
            criteria.AppId = GlobalVariable.user.ApplicationID;
            criteria.Device = GlobalVariable.user.Device;
            criteria.UserId = GlobalVariable.user.UserID;
            criteria.MAWBPrefix = txtPrefix.Text;
            criteria.MAWBSerial = txtSerial.Text;
            criteria.HAWB = txtHAWB.Text;
            //if (GlobalVariable.chkDatasource != "CARGOSPOT")
            //{
            //    criteria.Carrier = txtCarrier.Text;
            //    criteria.FlightNo = txtFltNo.Text;
            //}
            //else
            //{
            //    criteria.Carrier = null;
            //    criteria.FlightNo = null;
            //}
            criteria.Carrier = txtCarrier.Text;
            criteria.FlightNo = txtFltNo.Text;
            criteria.FlightDateString = fltdate_display;
            dataset = search.ValidateMAWB(criteria);
            status = dataset.Tables[0].Rows[0]["Status"].ToString();
            errorMessage = dataset.Tables[0].Rows[0]["Message"].ToString();
            int seq1 = dataset.Tables[0].Rows.Count + 1;
            if (status == "PASS")
            {
                if (txtHAWB.Text == dataset.Tables[0].Rows[0]["hawb"].ToString())
                {
                    checkconsol();
                    mawb = txtPrefix.Text + txtSerial.Text;
                    hawb = txtHAWB.Text;
                    prefix = txtPrefix.Text;
                    serial = txtSerial.Text;
                    Carrier = txtCarrier.Text;
                    fltNo = txtFltNo.Text;
                    fltno_display = Carrier + fltNo;
                    origin = txtOrigin.Text;
                    GlobalVariable.Origin = origin;
                    dest = txtDest.Text;
                    //DateFormat();
                    fltdate = txtFltdate.Text;
                    //string fltDate_display = fltdate + " " + txtFlttime.Text;
                    naturegood = txtNatureGood.Text;
                    piecs = txtPieces.Text;
                    if (ValueTofrmAddMAWB.Selshipment_type == "NONCON" && ValueTofrmAddMAWB.SelSource == "CARGOSPOT")
                    {
                        string[] array = new string[13] { prefix, serial, hawb, fltno_display, fltdate, origin, dest, naturegood, piecs, shipment_type, seq.ToString(), slip_type, ewsFlag };
                        main.getDataFromAddMAWB(array);
                        this.Close();
                    }
                    else
                    {
                        string[] array = new string[13] { prefix, serial, hawb, fltno_display, fltdate, origin, dest, naturegood, piecs, shipment_type, seq1.ToString(), slip_type, ewsFlag };
                        main.getDataFromAddMAWB(array);
                        this.Close();
                    }
                }
                else
                {
                    checkconsol();
                    mawb = txtPrefix.Text + txtSerial.Text;
                    hawb = txtHAWB.Text;
                    prefix = txtPrefix.Text;
                    serial = txtSerial.Text;
                    Carrier = txtCarrier.Text;
                    fltNo = txtFltNo.Text;
                    fltno_display = Carrier + fltNo;
                    origin = txtOrigin.Text;
                    GlobalVariable.Origin = origin;
                    dest = txtDest.Text;
                    //DateFormat();
                    fltdate = txtFltdate.Text;
                    //string fltDate_display = fltdate + " " + txtFlttime.Text;
                    naturegood = txtNatureGood.Text;
                    piecs = txtPieces.Text;
                    string[] array = new string[13] { prefix, serial, hawb, fltno_display, fltdate, origin, dest, naturegood, piecs, shipment_type, seq.ToString(), slip_type, ewsFlag };
                    main.getDataFromAddMAWB(array);
                    this.Close();
                }

            }
            else if (status == "WARNING")
            {
                if (errorMessage == "NO MAWB RECORDS. Error Code [WARNING-EICS-001]")
                {
                    if (GlobalVariable.chkFBL == "Gen")
                    {
                        checkconsol();
                        mawb = txtPrefix.Text + txtSerial.Text;
                        hawb = txtHAWB.Text;
                        prefix = txtPrefix.Text;
                        serial = txtSerial.Text;
                        Carrier = txtCarrier.Text;
                        fltNo = txtFltNo.Text;
                        fltno_display = Carrier + fltNo;
                        origin = txtOrigin.Text;
                        GlobalVariable.Origin = origin;
                        dest = txtDest.Text;
                        //DateFormat();
                        fltdate = txtFltdate.Text;
                        //string fltDate_display = fltdate + " " + txtFlttime.Text;
                        naturegood = txtNatureGood.Text;
                        piecs = txtPieces.Text;
                        string[] array = new string[13] { prefix, serial, hawb, fltno_display, fltdate, origin, dest, naturegood, piecs, shipment_type, seq.ToString(), slip_type, ewsFlag };
                        main.getDataFromAddMAWB(array);
                        CreateMAWBMessage();
                        this.Close();
                    }
                    else
                    {
                        checkconsol();
                        mawb = txtPrefix.Text + txtSerial.Text;
                        hawb = txtHAWB.Text;
                        prefix = txtPrefix.Text;
                        serial = txtSerial.Text;
                        Carrier = txtCarrier.Text;
                        fltNo = txtFltNo.Text;
                        fltno_display = Carrier + fltNo;
                        origin = txtOrigin.Text;
                        GlobalVariable.Origin = origin;
                        dest = txtDest.Text;
                        //DateFormat();
                        fltdate = txtFltdate.Text;
                        //string fltDate_display = fltdate + " " + txtFlttime.Text;
                        naturegood = txtNatureGood.Text;
                        piecs = txtPieces.Text;
                        string[] array = new string[13] { prefix, serial, hawb, fltno_display, fltdate, origin, dest, naturegood, piecs, shipment_type, seq.ToString(), slip_type, ewsFlag };
                        main.getDataFromAddMAWB(array);
                        this.Close();
                    }
                  
                }
                else
                {
                    CommonMessageBox.ShowMessageWarning(errorMessage);
                    this.Close();
                }
            }
            else
            {
                //errorMessage = dataset.Tables[0].Rows[0]["Message"].ToString();
                CommonMessageBox.ShowMessageWarning(errorMessage);
            }
        }

        private void cboSlipType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboSlipType.SelectedValue.ToString() == "LOCAL") // LOCAL
            {
                if (GlobalVariable.chkDatasource == "CARGOSPOT")
                {
                    txtOrigin.Text = "BKK";
                    txtDest.Text = ValueTofrmAddMAWB.Seldest;
                    //transhipment.Checked = false;
                    txtCarrier.Text = String.Empty;
                    txtFltNo.Text = String.Empty;
                    txtFltdate.Text = String.Empty;
                    txtNatureGood.Text = String.Empty;
                    slip_type = cboSlipType.SelectedValue.ToString();
                }
                else
                {
                    txtOrigin.Text = "BKK";
                    //transhipment.Checked = false;
                    txtCarrier.Text = String.Empty;
                    txtFltNo.Text = String.Empty;
                    txtFltdate.Text = String.Empty;
                    txtNatureGood.Text = String.Empty;
                    slip_type = cboSlipType.SelectedValue.ToString();
                }
            }
            //else if (cboSlipType.SelectedValue.ToString() == "RE-STORAGE") //RE-STORAGE
            //{

            //}
            else if (cboSlipType.SelectedValue.ToString() == "TRANSHIPMENT") //TRANSHIPMENT 
            {
                if (GlobalVariable.chkDatasource == "CARGOSPOT")
                {
                    DateFormat();
                    txtCarrier.Text = "BFS";
                    txtFltNo.Text = "001";
                    txtFltdate.Text = fltdate_display;
                    txtNatureGood.Text = "TRANS";
                    txtOrigin.Text = ValueTofrmAddMAWB.SelOrigin;
                    txtDest.Text = ValueTofrmAddMAWB.Seldest;
                    slip_type = cboSlipType.SelectedValue.ToString();
                }
                else
                {
                    DateFormat();
                    txtCarrier.Text = "BFS";
                    txtFltNo.Text = "001";
                    txtFltdate.Text = fltdate_display;
                    txtNatureGood.Text = "TRANS";
                    txtOrigin.Text = string.Empty;
                    txtDest.Text = string.Empty;
                    slip_type = cboSlipType.SelectedValue.ToString();
                }
                //radioButton2.Checked = true;

            }
        }
        //private void checkMAWB()
        //{
        //    string mawb = string.Empty;
        //    string errorMessage = string.Empty;
        //    SqlCommand cmd = new SqlCommand();
        //    try
        //    {
        //        mawb = txtPrefix.Text + txtSerial.Text;
        //        //string prefix = mawb.Substring(0, 3);
        //        //string serial = mawb.Substring(3, 8);
        //        cmd.CommandText = "usp_GetMAWB";
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Connection = sqlCon;
        //        cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
        //        cmd.Parameters.AddWithValue("@in_vchMAWB", mawb);
        //        cmd.Parameters.AddWithValue("@in_vchMAWBPrefix", txtPrefix.Text);
        //        cmd.Parameters.AddWithValue("@in_vchMAWBSerial", txtSerial.Text);
        //        cmd.Parameters.AddWithValue("@in_vchHAWB", txtHAWB.Text);
        //        cmd.Parameters.AddWithValue("@in_vchDevice", GlobalVariable.Device);
        //        cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
        //        sqlCon.Open();
        //        SqlDataAdapter da = new SqlDataAdapter(cmd);
        //        dataset = new DataSet();
        //        da.Fill(dataset, "testGetMAWB");
        //        // string mawb_sn = dataset.Tables[0].Rows[0]["mawb_sn"].ToString();
        //        string status = dataset.Tables[0].Rows[0]["Status"].ToString();
        //        int seq1 = dataset.Tables[0].Rows.Count + 1;
        //        if (status == "PASS")
        //        {
        //            if (txtHAWB.Text == dataset.Tables[0].Rows[0]["hawb"].ToString())
        //            {
        //                checkconsol();
        //                mawb = txtPrefix.Text + txtSerial.Text;
        //                hawb = txtHAWB.Text;
        //                prefix = txtPrefix.Text;
        //                serial = txtSerial.Text;
        //                Carrier = txtCarrier.Text;
        //                fltNo = txtFltNo.Text;
        //                fltno_display = Carrier + fltNo;
        //                dest = txtDest.Text;
        //                sqlCon.Close();
        //                //DateFormat();
        //                fltdate = txtFltdate.Text;
        //                //string fltDate_display = fltdate + " " + txtFlttime.Text;
        //                naturegood = txtNatureGood.Text;
        //                piecs = txtPieces.Text;
        //                if(ValueTofrmAddMAWB.Selshipment_type == "NONCON" && ValueTofrmAddMAWB.SelSource == "CARGOSPOT")
        //                {
        //                    string[] array = new string[10] { prefix, serial, hawb, fltno_display, fltdate, dest, naturegood, piecs, shipment_type, seq.ToString() };
        //                    main.getDataFromAddMAWB(array);
        //                    this.Dispose();
        //                }
        //                else
        //                {
        //                    string[] array = new string[10] { prefix, serial, hawb, fltno_display, fltdate, dest, naturegood, piecs, shipment_type, seq1.ToString() };
        //                    main.getDataFromAddMAWB(array);
        //                    this.Dispose();
        //                }     
        //            }
        //            else
        //            {
        //                checkconsol();
        //                mawb = txtPrefix.Text + txtSerial.Text;
        //                hawb = txtHAWB.Text;
        //                prefix = txtPrefix.Text;
        //                serial = txtSerial.Text;
        //                Carrier = txtCarrier.Text;
        //                fltNo = txtFltNo.Text;
        //                fltno_display = Carrier + fltNo;
        //                dest = txtDest.Text;
        //                sqlCon.Close();
        //                //DateFormat();
        //                fltdate = txtFltdate.Text;
        //                //string fltDate_display = fltdate + " " + txtFlttime.Text;
        //                naturegood = txtNatureGood.Text;
        //                piecs = txtPieces.Text;
        //                string[] array = new string[10] { prefix, serial, hawb, fltno_display, fltdate, dest, naturegood, piecs, shipment_type, seq.ToString() };
        //                main.getDataFromAddMAWB(array);
        //                this.Dispose();
        //            }

        //        }
        //        else if (status == "WARNING")
        //        {
        //            checkconsol();
        //            mawb = txtPrefix.Text + txtSerial.Text;
        //            hawb = txtHAWB.Text;
        //            prefix = txtPrefix.Text;
        //            serial = txtSerial.Text;
        //            Carrier = txtCarrier.Text;
        //            fltNo = txtFltNo.Text;
        //            fltno_display = Carrier + fltNo;
        //            dest = txtDest.Text;
        //            sqlCon.Close();
        //            //DateFormat();
        //            fltdate = txtFltdate.Text; 
        //            //string fltDate_display = fltdate + " " + txtFlttime.Text;
        //            naturegood = txtNatureGood.Text;
        //            piecs = txtPieces.Text;
        //            string[] array = new string[10] { prefix, serial, hawb, fltno_display, fltdate, dest, naturegood, piecs, shipment_type, seq.ToString() };
        //            main.getDataFromAddMAWB(array);
        //            CreateMAWBMessage();
        //            this.Dispose();
        //        }
        //        else
        //        {
        //            errorMessage = dataset.Tables[0].Rows[0]["Message"].ToString();
        //            CommonMessageBox.ShowMessageWarning(errorMessage);
        //        }
        //        //if (mawb_sn != "22222222")
        //        //{
        //        //    MessageBox.Show("It already have this MAWB in Database");
        //        //}
        //        //else if (mawb_sn == "22222222")
        //        //{
        //        //    checkconsol();
        //        //    mawb = txtPrefix.Text + txtSerial.Text;
        //        //    hawb = txtHAWB.Text;
        //        //    prefix = txtPrefix.Text;
        //        //    serial = txtSerial.Text;
        //        //    Carrier = txtCarrier.Text;
        //        //    fltNo = txtFltNo.Text;
        //        //    fltno_display = Carrier + fltNo;
        //        //    dest = txtDest.Text;
        //        //    sqlCon.Close();
        //        //    DateFormat();
        //        //    fltdate = fltdate_display;
        //        //    //string fltDate_display = fltdate + " " + txtFlttime.Text;
        //        //    naturegood = txtNatureGood.Text;
        //        //    piecs = txtPieces.Text;
        //        //    string[] array = new string[10] { prefix,serial, hawb, fltno_display, fltdate, dest, naturegood, piecs, shipment_type, seq.ToString() };
        //        //    main.getDataFromAddMAWB(array);
        //        //    CreateMAWBMessage();
        //        //    this.Dispose();

        //        //}
        //    }
        //    catch (Exception ex)
        //    {
        //        //string str = "You must enter 11 digits of MAWB";
        //        //MessageBox.Show(str);
        //        CommonMessageBox.ShowMessageError(ex);
        //    }
        //    finally
        //    {
        //        if (sqlCon != null && sqlCon.State == ConnectionState.Open)
        //        {
        //            sqlCon.Close();
        //        }
        //    }
        //}
        #endregion
    }
}
